import {UserApi} from '@/api/user/UserApi'
import {Config} from '@/Config'


export default {
    namespaced: true,
    state() {
        return {
            uid: '',
            mobile: '',
            credit: '',
            nickname: '',
            avatar: '',
            securityDeposit: '',
            accountBalance: '0.00',
            sellerId: '',
            collectCount: '0',
            vipType:null,
            vipExpiredTime:null,
            vipGrade:null,
            agencyGrade:null,
            agencyGradeName:null,
            vipGradeName:null,

        }
    },
    mutations: {
        setInfo(state, payload) {
            state.uid = payload.uid
            state.mobile = payload.mobile
            state.credit = payload.credit
            state.nickname = payload.nickname
            state.avatar = payload.avatar ? payload.avatar : Config.OSS_HOST+Config.AVATAR
            state.securityDeposit = payload.securityDeposit
            state.accountBalance = payload.accountBalance ? (payload.accountBalance/100).toFixed(2) : '0.00'
            state.sellerId = payload.sellerId
            state.collectCount = payload.collectCount ? payload.collectCount : 0
            state.vipType = payload.vipType
            state.vipExpiredTime = payload.vipExpiredTime
            state.vipGrade = payload.vipGrade
            state.agencyGrade = payload.agencyGrade
            state.agencyGradeName = payload.agencyGradeName
            state.vipGradeName = payload.vipGradeName
        },
    },
    actions: {
        getInfo({commit}, state) {
            return new Promise((resolve, reject) => {
                UserApi.getTokenInfo().then((response) => {
                    commit('setInfo', response)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        removeInfo({commit}, response){
            commit('setInfo', {})
        }
    }
}